package xfsale.service.impl.j2ee;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import xfsale.exception.AppException;
import xfsale.packet.CustomerRequest;
import xfsale.packet.CustomerResponse;

public final class Customer extends BaseService {

    private CustomerRequest request;
    private CustomerResponse response;

    public Customer(CustomerRequest request, CustomerResponse response) {
        super(request, response);

        this.request = request;
        this.response = response;

        response.returnCode = "0000";
        response.returnMsg = "客户身份识别成功";
    }

    //检查输入数据
    @Override
    protected void checkData() throws Exception {

        if (!request.identifyType.matches("^[0-9]{1,2}$")) {
            throw new AppException("身份识别方式不合法");
        }
    }

    //功能处理
    @Override
    protected void process() throws Exception {
        logger.info("开始识别客户身份...");

        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            // 01-交易账号
            if (request.identifyType.equals("01")) {
                stmt = conn.prepareStatement(""
                        + "select t.transAccountID, c.* "
                        + "from AcctTrans t, AcctCust c "
                        + "where t.transAccountID = ? "
                        + "and t.custAccountID = c.custAccountID ");
            }
            // 02-基金账号
            else if (request.identifyType.equals("02")) {
                stmt = conn.prepareStatement(""
                        + "select t.transAccountID, c.* "
                        + "from AcctTrans t, AcctCust c, AcctTA f "
                        + "where f.taAccountID = ? "
                        + "and t.custAccountID = c.custAccountID "
                        + "and f.transAccountID = t.transAccountID ");
            }
            // 11-身份证
            else if (request.identifyType.equals("11")) {
                stmt = conn.prepareStatement(""
                        + "select t.transAccountID, c.* "
                        + "from AcctTrans t, AcctCust c"
                        + "where c.custType='1' and c.certType='1' and certNo = ? "
                        + "and t.custAccountID = c.custAccountID "
                        + "and f.transAccountID = t.transAccountID ");
            }
            // 21-营业执照
            else if (request.identifyType.equals("21")) {
                stmt = conn.prepareStatement(""
                        + "select t.transAccountID, c.* "
                        + "from AcctTrans t, AcctCust c"
                        + "where c.custType='0' and c.certType='1' and certNo = ? "
                        + "and t.custAccountID = c.custAccountID "
                        + "and f.transAccountID = t.transAccountID ");
            }
            else {
					throw new AppException("不支持的身份识别方式");
			}
			stmt.setString(1, request.identifyCode);
            rs = stmt.executeQuery();
            if (!rs.next()) {
                throw new AppException("客户身份识别失败");
            }

            response.transAccountID = rs.getString("transAccountID");
            response.custAccountID = rs.getString("custAccountID");
            response.custName = rs.getString("custName");
            response.custNameAbbr = rs.getString("custNameAbbr");
            response.custType = rs.getString("custType");
            response.nationality = rs.getString("nationality");
            response.certType = rs.getString("certType");
            response.certNo = rs.getString("certNo");

            logger.debug("识别客户身份成功.");
        } catch (Exception e) {
            logger.error("识别客户身份出错. " + e);
            throw e;
        } finally {
            stmt.close();
        }
    }
}
